package com.LearnInWorld.search.controller;


import com.LearnInWorld.entity.Page;
import com.LearnInWorld.es.pojo.VideoInfo;
import com.LearnInWorld.search.service.SearchService;
import com.LearnInWorld.video.feign.ModuleFeign;
import com.LearnInWorld.video.pojo.Module;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/search")
public class SearchController {

    @Autowired
    private SearchService searchService;
    @Autowired
    private ModuleFeign moduleFeign;

    /**
     * 全文检索
     *
     * @param searchMap
     * @return
     */
    @GetMapping("/map")
    @ResponseBody
    public Map search(@RequestParam(required = false) Map<String, String> searchMap) {
        handlerSearchMap(searchMap);
        Map search = searchService.search(searchMap);
        return search;
    }

    //对搜索入参带有特殊符号进行处理
    public void handlerSearchMap(Map<String, String> searchMap) {
        if (searchMap != null) {
            for (Map.Entry<String, String> entry : searchMap.entrySet()) {
                if (entry.getKey().startsWith("spec_")) {
                    searchMap.put(entry.getKey(), entry.getValue().replace("+", "%2B"));
                }
            }
        }
    }

    /**
     * 查询
     *
     * @param searchMap
     * @param model
     * @return
     */
    @GetMapping("/list")
    public String list(@RequestParam Map<String, String> searchMap, Model model) {
        this.handlerSearchMap(searchMap);
        Map search = searchService.search(searchMap);
        model.addAttribute("resultList", search);
        model.addAttribute("searchMap", searchMap);

        //封装分页数据 、总记录数 、每页显示多少条 、当前页
        Page<VideoInfo> page = new Page<VideoInfo>();
        page.initPage(Long.parseLong(String.valueOf(search.get("total"))),
                Integer.parseInt(String.valueOf(search.get("pageNum"))),
                Page.pageSize);

        model.addAttribute("page", page);

        //拼接Url
        StringBuilder url = new StringBuilder("/api/search/list");
        if (searchMap != null && searchMap.size() > 0) {
            //拼接查询条件
            url.append("?");
            for (String key : searchMap.keySet()) {
                if (!key.equalsIgnoreCase("sortField")
                        && !key.equalsIgnoreCase("sortRule")
                        && !key.equalsIgnoreCase("pageNum")) {
                    url.append(key).append("=").append(searchMap.get(key)).append("&");
                }
            }
            String urlStr = url.toString();
            urlStr = urlStr.substring(0, urlStr.length() - 1);
            model.addAttribute("url", urlStr);
        } else {
            model.addAttribute("url", url);
        }

        //渲染模块数据区域
        Module module = new Module();
        module.setModuleName("全部");
        List<Module> moduleList = new ArrayList<>();
        moduleList.add(module);
        List<Module> resultList = moduleFeign.findAll().getData();
        for (Module m : resultList) {
            moduleList.add(m);
        }
        model.addAttribute("moduleList", moduleList);

        return "course_list";
    }

}
